﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TownCrier.Infrastructure.Entities;
using TownCrier.Channels;
using CookComputing.XmlRpc;
using TownCrier.Infrastructure.Contracts;
using TownCrier.Channels.ChannelTools;
using CassiniDev;

namespace TownCrier.Tests.Channels {
    /// <summary>
    /// Summary description for MetaweblogChannelTests
    /// </summary>
    [TestClass]
    public class MetaweblogChannelTests : RestChannelTestBase {

        //private Event _input;
        //private static string _postid;
        private static MetaweblogChannelSection _settings;
        //private static CassiniDevServer _server;

        public MetaweblogChannelTests() {
            //DateTime eventstart = DateTime.UtcNow.AddDays(2);

            //_input =  new Event {
            //    Title = "Test event", Topic = "Moles", Start = eventstart, End = eventstart.AddHours(4),
            //    Description = "This is a test blog post that has some information in it",
            //    URL = new Uri("http://www.anthroserbia.org"),
            //    UserGroup = "Dev UG",
            //    Venue = new Venue {
            //        Name = "MSO",
            //        City = "Belgrade",
            //        Street = "Makedonska 30",
            //    }
            //};

            _settings = System.Configuration.ConfigurationManager.GetSection("MetaweblogChannelSection") as MetaweblogChannelSection;

        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext {
            get {
                return testContextInstance;
            }
            set {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext) {
            string apppath = @"..\..\..\TownCrier.Tests\intpages";
            _server = new CassiniDevServer();
            _server.StartServer(apppath, 22222, "/", "localhost");
        }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        [ClassCleanup()]
        public static void MyClassCleanup() {
            //IMetaWeblog proxy = XmlRpcProxyGen.Create<IMetaWeblog>();
            //proxy.Url = _settings.ApiBaseUrl.Value;
            // delete the post
            //if (!String.IsNullOrEmpty(_postid))
            //    proxy.deletePost("", _postid, "blackdwarf", "pass@w0rd", false);

            if (_server != null) {
                _server.StopServer();
                _server.Dispose();
            }

        
        }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void MetaweblogChannelShouldThrowWhenEventIsInvalid() {
            var channel = new MetaWeblogChannel();

            var result = channel.TransmitMessage(null);

        }

        [TestMethod]
        public void MetaweblogChannelShouldDeliverAnEvent() {
            var channel = new MetaWeblogChannel();

            var result = channel.TransmitMessage(_sourceEvent);


            //var proxy = XmlRpcProxyGen.Create<IMetaWeblog>();
            //proxy.Url = _settings.ApiBaseUrl.Value;
            //var post = proxy.getPost(result.Message, _settings.HttpBasicAuth.Username, _settings.HttpBasicAuth.Password);

            Assert.IsTrue(result.Success);
            //Assert.IsNotNull(post);

        }

        [TestMethod]
        public void MetaWeblogChannelShouldImplementIchannel() {
            var channel = new MetaWeblogChannel();

            Assert.IsInstanceOfType(channel, typeof(IChannel));

        }

        [TestMethod]
        public void MetaweblogChannelShouldNotThrowOnfailure() {
            //MetaweblogChannelSection section = System.Configuration.ConfigurationManager.GetSection("MetaweblogChannelSection") as MetaweblogChannelSection;
            MetaweblogChannelSection section = new MetaweblogChannelSection();
            section.ApiBaseUrl.Value = "http://tempuri.org";
            section.BlogID = "testblog";

            var channel = new MetaWeblogChannel(XmlRpcProxyGen.Create<IMetaWeblog>(), section);

            var result = channel.TransmitMessage(_sourceEvent);

            Assert.IsFalse(result.Success);
        }

        [TestMethod]
        public void MetaweblogChannelSectionShouldContainExceptionXmlOnfailure() {
            Event input = new Event { Title = null, Description = "something" };
            var channel = new MetaWeblogChannel();

            var result = channel.TransmitMessage(input);

            Assert.IsFalse(result.Success);
            //Assert.IsNotNull(result.Message);
            Assert.IsTrue(result.Message.Contains("Member \"title\" of struct \"Post\" cannot be null."));
        }
    }
}
